package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.ReviewDTO;
import com.movie.dto.PageResponse;
import com.movie.service.ReviewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reviews")
@RequiredArgsConstructor
@Tag(name = "审核管理", description = "内容审核的增删改查接口")
public class ReviewController {

    private final ReviewService reviewService;

    @GetMapping("/pending")
    @Operation(summary = "获取待审核列表", description = "获取所有待审核的内容列表")
    public ApiResponse<PageResponse<ReviewDTO>> getPendingReviews(
            @Parameter(description = "页码，从0开始") @RequestParam(defaultValue = "0") int page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size,
            @Parameter(description = "审核类型") @RequestParam(required = false) String type,
            @Parameter(description = "来源") @RequestParam(required = false) String source) {
        Pageable pageable = PageRequest.of(page, size);
        Page<ReviewDTO> reviews = reviewService.findPendingReviews(type, source, pageable);
        return ApiResponse.success(PageResponse.of(reviews));
    }

    @PostMapping("/batch-review")
    @Operation(summary = "批量审核", description = "批量审核多个内容")
    public ApiResponse<Void> batchReview(
            @Parameter(description = "审核请求") @RequestBody List<ReviewDTO> reviewRequest) {
        try {
            for (ReviewDTO reviewDTO : reviewRequest) {
                reviewService.review(reviewDTO);
            }
            return ApiResponse.success("批量审核完成", null);
        } catch (Exception e) {
            return ApiResponse.error("批量审核失败");
        }
    }

    @PutMapping("/review")
    @Operation(summary = "审核单个内容", description = "审核单个内容")
    public ApiResponse<ReviewDTO> reviewComment(
            @Parameter(description = "审核请求") @RequestBody ReviewDTO reviewRequest) {

        ReviewDTO reviewed = reviewService.review(reviewRequest);
        return ApiResponse.success("审核完成", reviewed);
    }

    @GetMapping("/{reviewId}")
    @Operation(summary = "获取审核详情", description = "根据ID获取审核详情")
    public ApiResponse<ReviewDTO> getReviewDetail(
            @Parameter(description = "审核ID") @PathVariable Long reviewId) {
        ReviewDTO review = reviewService.findById(reviewId);
        return ApiResponse.success(review);
    }

    @DeleteMapping("/{reviewId}")
    @Operation(summary = "删除审核记录", description = "根据ID删除审核记录")
    public ApiResponse<Void> deleteReview(
            @Parameter(description = "审核ID") @PathVariable Long reviewId) {
        reviewService.delete(reviewId);
        return ApiResponse.success("审核记录删除成功", null);
    }

    @GetMapping("/stats")
    @Operation(summary = "获取审核统计", description = "获取审核统计信息")
    public ApiResponse<Map<String, Object>> getReviewStats() {
        Map<String, Object> stats = reviewService.getReviewStatistics();
        return ApiResponse.success(stats);
    }

    @GetMapping("/count")
    @Operation(summary = "获取评论总数", description = "获取评论总数统计")
    public ApiResponse<Map<String, Object>> getCommentCount() {
        // 这里需要实现获取评论总数的逻辑
        // 暂时返回空数据
        Map<String, Object> stats = Map.of(
                "totalComments", 0,
                "pendingComments", 0,
                "approvedComments", 0,
                "rejectedComments", 0
        );
        return ApiResponse.success(stats);
    }

    @GetMapping("/activities")
    @Operation(summary = "获取最近活动", description = "获取最近审核活动记录")
    public ApiResponse<List<Map<String, Object>>> getRecentActivities() {
        // 这里需要实现获取最近活动的逻辑
        // 暂时返回空列表
        List<Map<String, Object>> activities = List.of();
        return ApiResponse.success(activities);
    }
}